package org.lisa.crm.service.impl;

        import com.github.pagehelper.PageHelper;
        import com.github.pagehelper.PageInfo;
        import org.lisa.crm.common.util.Result;
        import org.lisa.crm.common.util.StringUtil;
        import org.lisa.crm.dao.MarketingDao;
        import org.lisa.crm.model.SaleChance;
        import org.lisa.crm.service.MarketingService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import java.util.Date;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

/**
 * Created by admin on 2018/1/8.
 */
@Service
public class MarketingServiceImpl implements MarketingService {

    @Autowired
    private MarketingDao marketingDao;

    @Override
    public Map<String, Object> querySaleChancesByParams(SaleChance saleChance) {
        Map<String,Object> map = new HashMap<>();
        if(StringUtil.isNotEmpty(saleChance.getCreateMan())){
            map.put("createMan",saleChance.getCreateMan());
        }
        if(StringUtil.isNotEmpty(saleChance.getCustomerName())){
            map.put("customerName",saleChance.getCustomerName());
        }
        if((saleChance.getCreateDate())!=null){
            map.put("createDate",saleChance.getCreateMan());
        }

        if((saleChance.getState())!=null){
            map.put("state",saleChance.getState());
        }




        List<SaleChance> list = marketingDao.querySaleChancesByParams(map);
        //组装分页参数
        PageHelper.startPage(saleChance.getPage(),saleChance.getRows());
        PageInfo<SaleChance> info = new PageInfo<>(list);
        Map<String, Object> map1 = new HashMap();
        map1.put("total", info.getTotal());
        map1.put("rows", info.getList());
        return map1;

    }

    @Override
    public Result insertSaleschance(SaleChance saleChance) {
        saleChance.setCreateDate(new Date());
        saleChance.setUpdateDate(new Date());
       int i =  marketingDao.insertSaleschance(saleChance);
        if(i==1){
            return Result.success("保存成功");
        }
        return Result.fail("保存失败");
    }

    public Result updateSaleschance(SaleChance saleChance){
        saleChance.setUpdateDate(new Date());
        int i =  marketingDao.updateSaleschance(saleChance);
        if(i==1){
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @Override
    public Result deleteSaleschance(int ids) {
        int i =  marketingDao.deleteSaleschance(ids);
        if(i==1){
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @Override
    public SaleChance querySalesChangeById(int sid) {

       return marketingDao.querySalesChangeById(sid);

    }

    @Override
    public Result updateSaleChanceDevResult(int devResult,int sid) {

        int i = marketingDao.updateSaleChanceDevResult(devResult,sid);
        if(i==1){
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }
}